package tech.xs.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.fastjson.JSONObject;

import tech.xs.entity.Staff;
import tech.xs.service.StaffService;
import tech.xs.service.impl.StaffServiceImpl;
@WebServlet("/add")
@SuppressWarnings("serial")
public class AddServlet extends HttpServlet {
	@SuppressWarnings("resource")
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("application/json;charset=UTF-8");
		ApplicationContext as= new ClassPathXmlApplicationContext("applicationContext.xml");
		StaffService staffSevice = (StaffService) as.getBean("staffServiceImpl");
		PrintWriter io= resp.getWriter();
		Integer empno=Integer.parseInt(req.getParameter("empno")) ;
		String ename =req.getParameter("ename");
		String job=req.getParameter("job");
		String date=req.getParameter("hiredate");
		Integer sal=Integer.parseInt(req.getParameter("sal")) ;
		Integer comm=Integer.parseInt(req.getParameter("comm")) ;
		String id_photo= req.getParameter("id_photo");
		Integer dept_id=Integer.parseInt(req.getParameter("dept_id")) ;
		Staff s=new Staff();
		s.setEmpno(empno);
		s.setEname(ename);
		s.setJob(job);
		s.setHiredate(date);
		s.setSal(sal);
		s.setComm(comm);
		s.setId_photo(id_photo);
		s.setDept_id(dept_id);
		boolean l= staffSevice.Add(s);
		JSONObject json=new JSONObject();
		if (l) {
			json.put("success", true);
		} else {
			json.put("success", false);
		}
		io.append(json.toString());
		io.flush();
		io.close();
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
